<script setup lang="ts" name="Logininfor">
import detailPage from "./detail.vue";
import { cleanLogininforApi, delLogininforApi, listLogininforApi, unlockLogininforApi } from "@/api/monitor/logininfor";

const { proxy } = getCurrentInstance();

const { sys_common_status } = useDict("sys_common_status");

// 遮罩层
const loading = ref(false);

// 导出遮罩层
const exportLoading = ref(false);

// 搜索栏元素
const queryLogininforRef = ref(null);

// 表单Id
const logininfor = ref<LogininforRecord>(null);

// 选中数组
const selection = ref<LogininforRecord[]>([]);

// 非单个禁用
const single = ref(true);

// 非多个禁用
const multiple = ref(true);

// 登陆日志表数据
const logininforList = ref<LogininforRecord[]>([]);

// 弹出层标题
const title = ref("");

// 详情弹出层
const openDetail = ref(false);

//总条数
const total = ref(0);

// 列信息
const columns = ref<TableColumnsRecord>({
  colNo: { label: "序号", visible: true },
  infoId: { label: "访问编号", visible: true },
  userName: { label: "用户名称", visible: true },
  ipaddr: { label: "地址", visible: true },
  loginLocation: { label: "登录地点", visible: true },
  os: { label: "操作系统", visible: true },
  browser: { label: "浏览器", visible: true },
  status: { label: "登录状态", visible: true },
  msg: { label: "描述", visible: true },
  loginTime: { label: "访问时间", visible: true },
});

// 访问时间选择
const dateRange = ref<[string, string]>(['', '']);

// 查询参数
const queryParams = ref<LogininforRecord>({
  pageNum: 1,
  pageSize: 10,
  infoId: null,
  userName: null,
  ipaddr: null,
  loginLocation: null,
  os: null,
  browser: null,
  status: null,
  msg: null,
  loginTime: null,
});

/** 查询登陆日志列表 */
function getList() {
  loading.value = true;
  listLogininforApi(addDateRange(queryParams.value, dateRange.value)).then(response => {
    logininforList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    logininforList.value.forEach((item, index) => {
      item['colNo'] = sequenceNo(index, queryParams.value);
    });
  });
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  if (refresh) {
    getList();
  }
  openDetail.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryLogininforRef.value.resetFields();
  dateRange.value = ['', ''];
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selectionList: LogininforRecord[]) {
  selection.value = selectionList;
  single.value = selectionList.length !== 1;
  multiple.value = !selectionList.length;
}


/** 详情按钮操作 */
function handleDetails(row: LogininforRecord) {
  logininfor.value = row;
  openDetail.value = true;
  title.value = "登陆日志详情";
}

/** 删除按钮操作 */
function handleDelete(row: LogininforRecord | any) {
  let ids = [];
  if (row.infoId) {
    ids = [row.infoId];
  } else {
    selection.value.forEach(item => {
      ids.push(item.infoId);
    })
  }

  // @ts-ignore
  proxy.$modal.confirm('是否确认删除访问编号为"' + ids + '"的数据项?', "系统提示", {
    type: "warning",
  }).then(() => {
    return delLogininforApi(ids);
  }).then(() => {
    getList();
    // @ts-ignore
    proxy.$modal.msgSuccess("删除成功");
  }).catch(err => {
    console.error(err)
  });
}

/** 清空按钮操作 */
function handleClean() {
  proxy.$modal.confirm("是否确认清空所有登录日志数据项?").then(function () {
    return cleanLogininforApi();
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("清空成功");
  }).catch(err => {
    console.error(err);
  });
}

/** 解锁按钮操作 */
function handleUnlock(row: LogininforRecord) {
  proxy.$modal.confirm('是否确认解锁用户"' + row.userName + '"数据项?').then(function () {
    return unlockLogininforApi(row.userName);
  }).then(() => {
    proxy.$modal.msgSuccess("用户" + row.userName + "解锁成功");
  }).catch(err => {
    console.error(err);
  });
}

/** 导出操作按钮 */
function handleExport() {
  download('monitor/logininfor/export', {
    ...queryParams.value
  }, `登陆日志_${new Date().getTime()}.xlsx`);
}

getList();
</script>

<template>
  <div class="app-container">
    <div class="back-item">
      <el-form :model="queryParams" ref="queryLogininforRef" :inline="true" label-width="68px">
        <el-form-item label="登陆地址" prop="ipaddr">
          <el-input
              v-model="queryParams.ipaddr"
              placeholder="请输入地址"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户名称"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="登录状态" prop="status">
          <el-select
              v-model="queryParams.status"
              placeholder="请选择登录状态"
              clearable
              @keyup.enter.native="handleQuery"
          >
            <el-option
                v-for="(item, index) in sys_common_status"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="访问时间" prop="loginTime">
          <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item class="item-search">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button type="warning" icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="back-item">
      <div class="table-top mb8">
        <div class="title title-left">
          登陆日志列表
        </div>
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-has-permi="['monitor:logininfor:remove']"
        >删除
        </el-button>
        <el-button
            type="danger"
            plain
            icon="Delete"
            @click="handleClean"
            v-hasPermi="['monitor:logininfor:remove']"
        >清空
        </el-button>
        <el-button
            type="warning"
            plain
            icon="Download"
            :loading="exportLoading"
            @click="handleExport"
            v-has-permi="['monitor:logininfor:export']"
        >导出
        </el-button>
        <span class="table-top-line" />
        <right-toolbar @queryTable="getList" :columns="columns" />
      </div>
      <el-table
          v-loading="loading"
          :data="logininforList"
          stripe
          v-tablesticky
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="55" prop="colNo" v-if="columns.colNo.visible" />
        <el-table-column label="访问编号" prop="infoId" align="center" v-if="columns.infoId.visible" />
        <el-table-column label="用户名称" prop="userName" align="center" v-if="columns.userName.visible" />
        <el-table-column label="地址" prop="ipaddr" align="center" min-width="140" show-overflow-tooltip
                         v-if="columns.ipaddr.visible" />
        <el-table-column label="登录地点" prop="loginLocation" align="center" v-if="columns.loginLocation.visible" />
        <el-table-column label="操作系统" prop="os" align="center" min-width="120" v-if="columns.os.visible" />
        <el-table-column label="浏览器" prop="browser" align="center" min-width="120" v-if="columns.browser.visible" />
        <el-table-column label="登录状态" prop="status" align="center" v-if="columns.status.visible">
          <template #default="scope">
            <dict-tag :options="sys_common_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="描述" prop="msg" align="center" min-width="160" v-if="columns.msg.visible" />
        <el-table-column label="访问时间" prop="loginTime" align="center" min-width="160" v-if="columns.loginTime.visible" />
        <el-table-column label="操作" align="center" fixed="right" min-width="120">
          <template #default="scope">
            <el-tooltip content="解锁" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Unlock"
                  @click="handleUnlock(scope.row)"
                  v-hasPermi="['monitor:logininfor:unlock']"
              />
            </el-tooltip>
            <el-tooltip content="详情" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Document"
                  @click="handleDetails(scope.row)"
                  v-has-permi="['monitor:logininfor:query']"
              />
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
                  class="text-danger"
                  v-has-permi="['monitor:logininfor:remove']"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          :total="total"
          open-fixed
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
      <div class="line-magin" />
    </div>

    <!-- 添加或修改登陆日志对话框 -->
    <detail-page v-if="openDetail" :row="logininfor" :title="title" @closeDialog="closeDialog" />
  </div>
</template>

<style lang="scss" scoped>

</style>